/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 05 - 21
 * Time: 18:37
 */
class ListNode {
    int val;
    ListNode next = null;
    ListNode() {
    }
    ListNode(int val) {
        this.val = val;
    }
}
public class Practice {
    public ListNode FindKthToTail(ListNode head, int k) {
        if ((k <= 0) || (head == null)) {
            return null;
        }
        ListNode fast = head;
        ListNode show = head;
        for (int i = 0; i < k - 1; i++) {
            fast = fast.next;
            if (fast == null) {//k(倒数的个数)太大，即找不到倒数的第k个数
                return null;
            }
        }
        while (fast.next != null) {
            fast = fast.next;
            show = show.next;
        }
        return show;
    }
}
class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode node = new ListNode();
        ListNode head1 = list1;
        ListNode head2 = list2;
        ListNode th = node;//定义一个链表中已经排好序的那部分的末尾节点的引用
        while((head1 != null) && (head2 != null)) {
            if(head1.val > head2.val) {
                th.next = head2;
                th = th.next;
                head2 = head2.next;
            }else {
                th.next = head1;
                th = th.next;
                head1 = head2.next;
            }
        }
        if(head1.next != null) {
            th.next = head1;
        }
        if(head2.next != null) {
            th.next = head2;
        }
        return node.next;
    }
}

